package cn.edu.ptu.jobfairs.basic.common;

import cn.edu.ptu.jobfairs.basic.enums.MemberStatus;
import cn.edu.ptu.jobfairs.core.utils.ValidUtils;
import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * Created by Lin Chenxiao on 2020-05-26
 **/
@Data
public class User implements UserDetails {
    private static final long serialVersionUID = 7327237917925743609L;
    private Integer id;
    private String number;
    private String pwMd5;
    private String name;
    private String title;
    private String phone;
    private Integer status;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Lists.newArrayList(new SimpleGrantedAuthority(MemberStatus.nameOf(status)));
    }

    @Override
    public String getPassword() {
        return pwMd5;
    }

    @Override
    public String getUsername() {
        return number;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return !ValidUtils.intEquals(status, MemberStatus.LOCKED.getValue());
    }

}
